<?php
class ThemesController extends AppController {

    public $helpers = array('Pagination');

    public function admin_index() {
        $this->Theme->recursive = 0;
        $this->set('themes', $this->paginate());
    }

    public function admin_switchto($id = null) {
        if($this->Theme->findById($id)) {
            $this->Theme->activate_theme($id);
            $this->Session->setFlash(__('The Theme has been activated',true));
        } else {
            $this->Session->setFlash(__('The Theme could not be activated. Please, try again.',true));
        }
        $this->redirect(array('action'=>'admin_index'));
    }

    public function admin_add() {
        if (!empty($this->data)) {
            $this->Theme->create();
            if ($this->Theme->save($this->data)) {
                $this->Session->setFlash(__('The Theme has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Theme could not be saved. Please, try again.', true));
            }
        }
    }

    public function admin_edit($id = null) {
        if (!$this->Theme->findById($id)) {
            $this->Session->setFlash(__('Invalid theme', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Theme->save($this->data)) {
                $this->Session->setFlash(__('The Theme has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Theme could not be saved. Please, try again.', true));
            }
        } else {
            $this->data = $this->Theme->read(null, $id);
        }
    }

    public function admin_delete($id = null) {
    //at least one theme must be active
        if (!$this->Theme->findById($id) || ($id==1)) {
            $this->Session->setFlash(__('Invalid theme', true));
        }
        if ($this->Theme->del($id)) {
            $this->Session->setFlash(__('Theme deleted'));
        } else {
            $this->Session->setFlash(__('The Theme could not be deleted. Please, try again.', true));
        }
        $this->redirect(array('action'=>'index'));
    }

    public function admin_editlayout($action = 'layout') {
        App::import('vendor','file');
        $folder = $this->Theme->find('active=1');
        if($action=='layout') {
            $path = '..'.DS.'views'.DS.'themed'.DS.$folder['Theme']['folder'].DS.'layouts'.DS.'default.ctp';
        } else {
            $path = 'themed'.DS.$folder['Theme']['folder'].DS.'css'.DS.'stylesheet.css';
        }
        if(!empty($this->data)) {
            $file = new File($path);
            if($file->write($this->data['Theme']['file'],'w',true)) {
                $this->Session->setFlash(__('The Layout has been saved',true));
            } else {
                $this->Session->setFlash(__('The Layout could not be saved. Please, try again.'));
            }
        } else {
            $file = new File($path);
            $this->set('file',$file->read());
        }
        $this->set('action',$action);
    }
}
?>